import {getNewsUsage} from '@/base/api/NewsApi';
import { calculateTrendLine } from "@/base/util/Dates";
export class DataBloc {
    startValue=0
    endValue=0
    optionsNewsUsage:object={
        color: ['#2d8cf0', '#91CC75', '#ee656e'],
        title: {
            text: '推送统计',
            left: 'center',
            top: 5
        },
        tooltip:{
            trigger: 'axis'
        },
        toolbox: {
            feature: {
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true }
            }
        },
        legend: {
            bottom:0,
            data: [ '平均线','数据展示','趋势线']
        },
        grid: [{ left: '5%', top: '10%', width: '90%', height: '80%' }],
        xAxis: [
            {
            name: '日期',
            type: 'category',
            boundaryGap: false,
            }
        ],
        yAxis: {
            name: '次数',
            type: 'value'
        },
        series: [
            {
                name: '平均线',
                type: 'line',
                data: []
            },
            {
                name: '数据展示',
                type: 'line',
                data: []
            },
            {
                name: '趋势线',
                type: 'line',
                markLine: {
                    animation: true,
                    lineStyle: {
                    type: 'dashed',
                    color: 'red'
                    },
                    data: [
                    [
                        {
                        coord: [0, 0],
                        symbol: 'none'
                        },
                        {
                        coord: [0, 0],
                        symbol: 'none'
                        }
                    ]
                    ]
                }
            },
        ]
    }
    
    dateNewsUsage = [
        new Date(new Date().getTime()-6*30*24*60*60*1000),
        new Date()
    ];
    async getNewsUsage(){
        let startTime:any=this.dateNewsUsage[0].getTime()
        let endTime:any=this.dateNewsUsage[1].getTime()
        return await getNewsUsage({
            startTime,
            endTime
        }).then((res)=>{
            // console.log(res.data);return;
            if(res.data.length<=0)return
            const dataList:any=[]
            const dataAvg:any=[]
            // 1. 提取num字段为一个数组
            const nums = res.data.map(item => item.num);
            // 2. 求和
            const sum = nums.reduce((total, cur) => total + cur, 0);
            // 3. 求平均值
            const avg = Math.ceil(sum / nums.length);
            res.data.map((item)=>{
                dataList.push([item.month,item.num])
                dataAvg.push([item.month,avg])
            })
            const trendLine=calculateTrendLine(res.data)
            // console.log('斜率:', trendLine.slope);
            // console.log('截距:', trendLine.intercept);
            // console.log('起始点值:', trendLine.startValue);
            // console.log('结束点值:', trendLine.endValue);
            this.startValue=trendLine.startValue
            this.endValue=trendLine.endValue
            this.optionsNewsUsage['series'][0]['data']=dataAvg
            this.optionsNewsUsage['series'][1]['data']=dataList
            this.optionsNewsUsage['series'][2]['markLine']['data'][0][0]['coord'][0]=dataList[0][0]
            this.optionsNewsUsage['series'][2]['markLine']['data'][0][0]['coord'][1]=this.startValue
            this.optionsNewsUsage['series'][2]['markLine']['data'][0][1]['coord'][0]=dataList[dataList.length-1][0]
            this.optionsNewsUsage['series'][2]['markLine']['data'][0][1]['coord'][1]=this.endValue
            console.log(dataList,trendLine,this.optionsNewsUsage)
        })
    }

}
